package com.quwan.manager.controller;

import com.quwan.common.BaseController;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TVipRule;
import com.quwan.manager.service.IVipRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author laozhang
 * @date 2022/4/24
 */
@Api(tags = "后台会员配置——金额配置管理接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("mgr/rule")
public class TVipRuleController extends BaseController {

    private final IVipRuleService vipRuleService;

    @ApiOperation(value = "金额配置列表",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("vipRuleList")
    public ApiResult<List<TVipRule>> vipRuleList() {
        List<TVipRule> res = vipRuleService.getTVipRuleList();
        return ApiResult.success(res);
    }

    @ApiOperation("金额配置修改——不需要传时间字段")
    @PostMapping("updateTVipRule")
    public ApiResult updateTVipRule(TVipRule vipRule){
        boolean success = vipRuleService.updateTVipRule(vipRule);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }
    @PostMapping("updateTVipRuleList")
    @ApiOperation("金额配置修改——批量——不需要传时间字段")
    public ApiResult updateTVipRuleList(List<TVipRule> vipRuleList){
        boolean success = true;
        for (TVipRule vipRule:vipRuleList) {
            vipRule.setAddTime(new Date());
            success = vipRuleService.updateTVipRule(vipRule);
        }
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("addTVipRule")
    @ApiOperation("添加金额配置——不需要传时间字段")
    public ApiResult addVipRule(TVipRule vipRule){
        vipRule.setAddTime(new Date());
        boolean success = vipRuleService.addTVipRule(vipRule);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("addTVipRuleList")
    @ApiOperation("添加金额配置——批量——不需要传时间字段")
    public ApiResult addVipRuleList(List<TVipRule> vipRuleList){
        boolean success = true;
        for (TVipRule vipRule:vipRuleList) {
            vipRule.setAddTime(new Date());
            success = vipRuleService.addTVipRule(vipRule);
        }
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @ApiOperation(value = "金额配置详情",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("detailTVipRule")
    public ApiResult detailVipRule(@RequestParam(value = "id") Integer id) {
        TVipRule VipRule = vipRuleService.detailTVipRule(id);
        return ApiResult.success(VipRule);

    }

    @PostMapping("delTVipRule")
    @ApiOperation("删除金额配置")
    public ApiResult delVipRule(@RequestParam(value = "id") Integer id){
        boolean success = vipRuleService.delTVipRule(id);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }
}
